package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.fc.entity.vo.FcTreeVo;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lg.soar.boot.fc.mapper.BuildingTypeMapper;
import cn.lg.soar.boot.fc.entity.BuildingType;
import cn.lg.soar.boot.fc.service.BuildingTypeService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 楼栋类型表
 * @author ZhuanZ（无密码）
 * @since 2025-02-15
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class BuildingTypeServiceImpl extends ServiceImpl<BuildingTypeMapper, BuildingType> implements BuildingTypeService {

    @Override
    public BuildingType getByName(String name) {
        if (StringUtils.isBlank(name)) {
            return new BuildingType();
        }
        Wrappers.<BuildingType>lambdaQuery().eq(BuildingType::getName, name);

        this.list(Wrappers.<BuildingType>lambdaQuery().eq(BuildingType::getName, name));

        List<BuildingType> list = this.list(Wrappers.<BuildingType>lambdaQuery().eq(BuildingType::getName,
                name));

        if (null == list || 0 >= list.size()) {
            return new BuildingType();
        }
        return list.get(0);
    }

    @Override
    public PageResult<FcTreeVo> queryResultVo(PageQuery pageQuery, QueryParams params) {
        PageResult<FcTreeVo> pageResultVo = new PageResult<>();

        SqueryWapper<BuildingType> squeryWapper = new SqueryWapper<>(BuildingType.class);
        PageResult<BuildingType> pageResult = squeryWapper.query(this, pageQuery, params);

        // 对象拷贝
        BeanUtils.copyProperties(pageQuery, pageResultVo);
        // 返回vo
        List<FcTreeVo> listVo = new ArrayList<>();
        pageResult.getList().forEach(item-> {
            FcTreeVo fcTreeVo = new FcTreeVo();
            BeanUtils.copyProperties(item, fcTreeVo);
            fcTreeVo.setParentId("0");
            fcTreeVo.setId(String.valueOf(item.getId()));
            listVo.add(fcTreeVo);
        });
        pageResultVo.setList(listVo);
        pageResultVo.setTotal(Long.valueOf(listVo.size()));
        return pageResultVo;
    }

    @Override
    public HttpResult<Serializable> updateSort(List<Map<String, Integer>> sortedData) {
        if (null == sortedData || 0 >= sortedData.size()) {
            return HttpResult.fail("4000", "排序数据不可为空", "error");
        }

        sortedData.forEach(item-> {

            if (!item.containsKey("id") || !item.containsKey("orders")) {
                return;
            }

            Long id = item.get("id").longValue();
            int orders = item.get("orders");

            BuildingType buildingType = new BuildingType();
            buildingType.setId(id);
            buildingType.setOrders(orders);
            this.updateById(buildingType);
        });

        return HttpResult.success("排序成功");
    }
}
